/*
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * This file is a part of the CANN Open Software.
 * Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */

#include <sstream>
#include "../../common/data_utils.h"
#include "kernel_tiling/kernel_tiling.h"
#include "tiling/platform/platform_ascendc.h"
#include "./op_host/matmul_a2_b2_share_custom_tiling.h"

#ifdef ASCENDC_CPU_DEBUG
#include "tikicpulib.h"
extern "C" void matmul_a2_b2_share_custom(
    uint8_t* a1, uint8_t* b1, uint8_t* a2, uint8_t* b2, uint8_t* bias, uint8_t* c1, uint8_t* c2,
    uint8_t* workspace, uint8_t* tiling);
#else
#include "acl/acl.h"
extern void matmul_a2_b2_share_custom_do(uint32_t coreDim, void* stream,
    uint8_t* a1, uint8_t* b1, uint8_t* a2, uint8_t* b2, uint8_t* bias, uint8_t* c1, uint8_t* c2,
    uint8_t* workspace, uint8_t* tiling);
#endif

namespace MatmulHost {

#ifdef ASCENDC_CPU_DEBUG
void TestMatmulCpu(const optiling::TestcaseParams& caseParams)
{
    auto ascendcPlatform = platform_ascendc::PlatformAscendCManager::GetInstance();
    matmul_tiling::MultiCoreMatmulTiling cubeTiling(*ascendcPlatform);
    uint32_t mixRatio = 2;  // AIC:AIV = 1:2
    uint32_t aivCoreNum = ascendcPlatform->GetCoreNum();
    uint32_t aicCoreNum = aivCoreNum / mixRatio;

    size_t aFileSize = static_cast<size_t>(sizeof(uint16_t) * caseParams.m * caseParams.k); // uint16_t represent half
    size_t bFileSize = static_cast<size_t>(sizeof(uint16_t) * caseParams.k * caseParams.n); // uint16_t represent half
    size_t biasFileSize = static_cast<size_t>(sizeof(float) * caseParams.n);
    size_t cFileSize = static_cast<size_t>(sizeof(float) * caseParams.m * caseParams.n);
    size_t workspaceSize = static_cast<size_t>(ascendcPlatform->GetLibApiWorkSpaceSize());
    size_t tilingFileSize = sizeof(TCubeTiling);

    uint8_t* a1 = (uint8_t *)AscendC::GmAlloc(aFileSize);
    uint8_t* b1 = (uint8_t *)AscendC::GmAlloc(bFileSize);
    uint8_t* a2 = (uint8_t *)AscendC::GmAlloc(aFileSize);
    uint8_t* b2 = (uint8_t *)AscendC::GmAlloc(bFileSize);
    uint8_t* bias;
    uint8_t* c1 = (uint8_t *)AscendC::GmAlloc(cFileSize);
    uint8_t* c2 = (uint8_t *)AscendC::GmAlloc(cFileSize);
    uint8_t* workspace = (uint8_t *)AscendC::GmAlloc(workspaceSize);
    uint8_t* tiling = (uint8_t *)AscendC::GmAlloc(tilingFileSize);

    ReadFile("../input/a1_gm.bin", aFileSize, a1, aFileSize);
    ReadFile("../input/b1_gm.bin", bFileSize, b1, bFileSize);
    ReadFile("../input/a2_gm.bin", aFileSize, a2, aFileSize);
    ReadFile("../input/b2_gm.bin", bFileSize, b2, bFileSize);
    if (caseParams.isBias) {
        bias = (uint8_t *)AscendC::GmAlloc(biasFileSize);
        ReadFile("../input/bias_gm.bin", biasFileSize, bias, biasFileSize);
    }

    optiling::GenerateTiling(aicCoreNum, &cubeTiling, caseParams, tiling);
    memcpy_s(tiling, tilingFileSize, tiling, tilingFileSize);

    ICPU_RUN_KF(matmul_a2_b2_share_custom, aicCoreNum, a1, b1, a2, b2, bias, c1, c2, workspace, tiling);

    WriteFile("../output/output_1.bin", c1, cFileSize);
    WriteFile("../output/output_2.bin", c2, cFileSize);

    AscendC::GmFree((void*)a1);
    AscendC::GmFree((void*)b1);
    AscendC::GmFree((void*)a2);
    AscendC::GmFree((void*)b2);
    if (caseParams.isBias) {
        AscendC::GmFree((void*)bias);
    }
    AscendC::GmFree((void*)c1);
    AscendC::GmFree((void*)c2);
    AscendC::GmFree((void*)workspace);
    AscendC::GmFree((void*)tiling);
}
#else
void MatmulOp(uint8_t* a1, uint8_t* b1, uint8_t* a2, uint8_t* b2, uint8_t* bias, uint8_t* c1, uint8_t* c2,
    const optiling::TestcaseParams& caseParams, void* stream = nullptr)
{
    auto ascendcPlatform = platform_ascendc::PlatformAscendCManager::GetInstance();
    matmul_tiling::MultiCoreMatmulTiling cubeTiling(*ascendcPlatform);
    uint32_t mixRatio = 2;  // AIC:AIV = 1:2
    uint32_t aivCoreNum = ascendcPlatform->GetCoreNum();
    uint32_t aicCoreNum = aivCoreNum / mixRatio;

    uint8_t *workspaceDevice;
    size_t workspaceSize = static_cast<size_t>(ascendcPlatform->GetLibApiWorkSpaceSize());
    CHECK_ACL(aclrtMalloc((void **)&workspaceDevice, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST));

    uint8_t* tilingHost;
    uint8_t* tilingDevice;
    size_t tilingFileSize = sizeof(TCubeTiling);
    CHECK_ACL(aclrtMallocHost((void **)(&tilingHost), tilingFileSize));
    CHECK_ACL(aclrtMalloc((void **)&tilingDevice, tilingFileSize, ACL_MEM_MALLOC_HUGE_FIRST));
    optiling::GenerateTiling(aicCoreNum, &cubeTiling, caseParams, tilingHost);
    CHECK_ACL(aclrtMemcpy(tilingHost, tilingFileSize, tilingHost, tilingFileSize, ACL_MEMCPY_HOST_TO_HOST));
    CHECK_ACL(aclrtMemcpy(tilingDevice, tilingFileSize, tilingHost, tilingFileSize, ACL_MEMCPY_HOST_TO_DEVICE));

    matmul_a2_b2_share_custom_do(aicCoreNum, stream, a1, b1, a2, b2, bias, c1, c2, workspaceDevice, tilingDevice);
    CHECK_ACL(aclrtSynchronizeStream(stream));

    CHECK_ACL(aclrtFreeHost(tilingHost));
    CHECK_ACL(aclrtFree(workspaceDevice));
    CHECK_ACL(aclrtFree(tilingDevice));
}

void TestAclInit(aclrtContext &context, aclrtStream &stream, int64_t &deviceId)
{
    CHECK_ACL(aclInit(nullptr));
    CHECK_ACL(aclrtSetDevice(deviceId));
    CHECK_ACL(aclrtCreateContext(&context, deviceId));
    CHECK_ACL(aclrtCreateStream(&stream));
}

void TestAclDeInit(aclrtContext &context, aclrtStream &stream, int64_t &deviceId)
{
    CHECK_ACL(aclrtDestroyStream(stream));
    CHECK_ACL(aclrtDestroyContext(context));
    CHECK_ACL(aclrtResetDevice(deviceId));
    CHECK_ACL(aclFinalize());
}

void TestMatmul(const optiling::TestcaseParams& caseParams)
{
    size_t aFileSize = static_cast<size_t>(sizeof(uint16_t) * caseParams.m * caseParams.k); // uint16_t represent half
    size_t bFileSize = static_cast<size_t>(sizeof(uint16_t) * caseParams.k * caseParams.n); // uint16_t represent half
    size_t biasFileSize = static_cast<size_t>(sizeof(float) * caseParams.n);
    size_t cFileSize = static_cast<size_t>(sizeof(float) * caseParams.m * caseParams.n);

    aclrtContext context;
    aclrtStream stream = nullptr;
    int64_t deviceId = 0;
    TestAclInit(context, stream, deviceId);

    uint8_t* a1Host;
    uint8_t* a1Device;
    CHECK_ACL(aclrtMallocHost((void **)(&a1Host), aFileSize));
    CHECK_ACL(aclrtMalloc((void **)&a1Device, aFileSize, ACL_MEM_MALLOC_HUGE_FIRST));
    ReadFile("../input/a1_gm.bin", aFileSize, a1Host, aFileSize);
    CHECK_ACL(aclrtMemcpy(a1Device, aFileSize, a1Host, aFileSize, ACL_MEMCPY_HOST_TO_DEVICE));

    uint8_t* b1Host;
    uint8_t* b1Device;
    CHECK_ACL(aclrtMallocHost((void **)(&b1Host), bFileSize));
    CHECK_ACL(aclrtMalloc((void **)&b1Device, bFileSize, ACL_MEM_MALLOC_HUGE_FIRST));
    ReadFile("../input/b1_gm.bin", bFileSize, b1Host, bFileSize);
    CHECK_ACL(aclrtMemcpy(b1Device, bFileSize, b1Host, bFileSize, ACL_MEMCPY_HOST_TO_DEVICE));

    uint8_t* a2Host;
    uint8_t* a2Device;
    CHECK_ACL(aclrtMallocHost((void **)(&a2Host), aFileSize));
    CHECK_ACL(aclrtMalloc((void **)&a2Device, aFileSize, ACL_MEM_MALLOC_HUGE_FIRST));
    ReadFile("../input/a2_gm.bin", aFileSize, a2Host, aFileSize);
    CHECK_ACL(aclrtMemcpy(a2Device, aFileSize, a2Host, aFileSize, ACL_MEMCPY_HOST_TO_DEVICE));

    uint8_t* b2Host;
    uint8_t* b2Device;
    CHECK_ACL(aclrtMallocHost((void **)(&b2Host), bFileSize));
    CHECK_ACL(aclrtMalloc((void **)&b2Device, bFileSize, ACL_MEM_MALLOC_HUGE_FIRST));
    ReadFile("../input/b2_gm.bin", bFileSize, b2Host, bFileSize);
    CHECK_ACL(aclrtMemcpy(b2Device, bFileSize, b2Host, bFileSize, ACL_MEMCPY_HOST_TO_DEVICE));

    uint8_t* biasHost;
    uint8_t* biasDevice;
    if (caseParams.isBias) {
        CHECK_ACL(aclrtMallocHost((void **)(&biasHost), biasFileSize));
        CHECK_ACL(aclrtMalloc((void **)&biasDevice, biasFileSize, ACL_MEM_MALLOC_HUGE_FIRST));
        ReadFile("../input/bias_gm.bin", biasFileSize, biasHost, biasFileSize);
        CHECK_ACL(aclrtMemcpy(biasDevice, biasFileSize, biasHost, biasFileSize, ACL_MEMCPY_HOST_TO_DEVICE));
    }

    uint8_t* c1Host;
    uint8_t* c1Device;
    CHECK_ACL(aclrtMallocHost((void **)(&c1Host), cFileSize));
    CHECK_ACL(aclrtMalloc((void **)&c1Device, cFileSize, ACL_MEM_MALLOC_HUGE_FIRST));

    uint8_t* c2Host;
    uint8_t* c2Device;
    CHECK_ACL(aclrtMallocHost((void **)(&c2Host), cFileSize));
    CHECK_ACL(aclrtMalloc((void **)&c2Device, cFileSize, ACL_MEM_MALLOC_HUGE_FIRST));

    MatmulOp(a1Device, b1Device, a2Device, b2Device, biasDevice, c1Device, c2Device, caseParams, stream);

    CHECK_ACL(aclrtMemcpy(c1Host, cFileSize, c1Device, cFileSize, ACL_MEMCPY_DEVICE_TO_HOST));
    WriteFile("../output/output_1.bin", c1Host, cFileSize);
    CHECK_ACL(aclrtMemcpy(c2Host, cFileSize, c2Device, cFileSize, ACL_MEMCPY_DEVICE_TO_HOST));
    WriteFile("../output/output_2.bin", c2Host, cFileSize);

    CHECK_ACL(aclrtFree(a1Device));
    CHECK_ACL(aclrtFreeHost(a1Host));
    CHECK_ACL(aclrtFree(b1Device));
    CHECK_ACL(aclrtFreeHost(b1Host));

    CHECK_ACL(aclrtFree(a2Device));
    CHECK_ACL(aclrtFreeHost(a2Host));
    CHECK_ACL(aclrtFree(b2Device));
    CHECK_ACL(aclrtFreeHost(b2Host));

    CHECK_ACL(aclrtFree(c1Device));
    CHECK_ACL(aclrtFreeHost(c1Host));
    CHECK_ACL(aclrtFree(c2Device));
    CHECK_ACL(aclrtFreeHost(c2Host));

    TestAclDeInit(context, stream, deviceId);
}
#endif
} // end namespace MatmulHost

int32_t main(int32_t argc, const char* args[])
{
    uint32_t problem[4];
    for (int32_t i = 1; i < argc && i < 5; ++i) {
        std::stringstream ss(args[i]);
        ss >> problem[i - 1];
    }

    optiling::TestcaseParams caseParams = {problem[0], problem[1], problem[2], problem[3]};
    auto ascendcPlatform = platform_ascendc::PlatformAscendCManager::GetInstance(SOC_VERSION);
#ifdef ASCENDC_CPU_DEBUG
    MatmulHost::TestMatmulCpu(caseParams);
#else
    MatmulHost::TestMatmul(caseParams);
#endif
    return 0;
}